package com.dfl.ggbond.guidepage;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dfl.ggbond.MainActivity;
import com.dfl.ggbond.R;

public class GuidePageAdapter extends RecyclerView.Adapter<GuidePageAdapter.GuidePageViewHolder> {

    @NonNull
    @Override
    public GuidePageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.guide_page, parent, false);
        return new GuidePageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GuidePageViewHolder holder, int position) {
        holder.itemView.setTag("page" + position);

        if (position == 2) {
            holder.itemView.findViewById(R.id.btn_skip).setVisibility(View.VISIBLE);
            holder.itemView.findViewById(R.id.btn_skip).setOnClickListener(v -> {
                android.content.Context context = holder.itemView.getContext();
                android.content.Intent intent = new android.content.Intent(context, MainActivity.class);
                context.startActivity(intent);
                if (context instanceof android.app.Activity) {
                    ((android.app.Activity) context).finish();
                }
            });
        } else {
            holder.itemView.findViewById(R.id.btn_skip).setVisibility(View.GONE);
            holder.itemView.findViewById(R.id.btn_skip).setOnClickListener(null);
        }

        // 不再对页面背景赋固定颜色，只设置图片与文字
        switch (position) {
            case 0:
                holder.imageView.setImageResource(R.drawable.ic_home_image);
                holder.textView.setText("图像信息多模式快速查看");
                break;
            case 1:
                holder.imageView.setImageResource(R.drawable.ic_home_video);
                holder.textView.setText("视频信息全模式支持急速播放");
                break;
            case 2:
            default:
                holder.imageView.setImageResource(R.drawable.ic_home_doc);
                holder.textView.setText("文档信息全格式快速搜索");
                break;
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    public class GuidePageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public GuidePageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.guide_image);
            textView = itemView.findViewById(R.id.guide_text);
        }
    }
}